package cn.itsource.pet.service.impl;

import cn.itsource.basic.constant.PetHomeConstant;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AxajResult;
import cn.itsource.basic.util.CodeGenerateUtils;
import cn.itsource.basic.util.DistanceUtil;
import cn.itsource.basic.util.Point;
import cn.itsource.order.domain.PetAcquisitionOrder;
import cn.itsource.order.mapper.PetAcquisitionOrderMapper;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.pet.service.IPetService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import sun.rmi.runtime.Log;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;
    @Override
    public void save(Pet pet,Employee loginUser) {
        //保存宠物信息
        super.save(pet);
        //获取宠物详情
        PetDetail detail = pet.getDetail();
        //设置pet
        detail.setPet(pet);
        //保存详情
        petDetailMapper.save(detail);
        //获取寻主人id
        Long id = pet.getSearchMasterMsg().getId();
        //通过id查询对象
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findById(id);
        //设置状态为已处理
        searchMasterMsg.setState(PetHomeConstant.OK);
        //修改寻主信息
        searchMasterMsgMapper.update(searchMasterMsg);
        //给pet重新设置寻主信息
        pet.setSearchMasterMsg(searchMasterMsg);
        //创建收购订单
        PetAcquisitionOrder order=createPetAcquisitionOrder(pet,loginUser);
        //保存收购订单信息
        petAcquisitionOrderMapper.save(order);
    }

    private PetAcquisitionOrder createPetAcquisitionOrder(Pet pet,Employee loginUser) {
        //创建收购订单
        PetAcquisitionOrder petOrder = new PetAcquisitionOrder();
        //设置摘要
        petOrder.setDigest("[摘要]对"+pet.getSearchMasterMsg().getName()+"收购订单！");
        //设置收购地址
        petOrder.setAddress(pet.getSearchMasterMsg().getAddress());
        //设置付款方式
        petOrder.setPaytype(pet.getPaytype());
        //设置收购价格
        petOrder.setPrice(pet.getCostprice());
        //设置订单编号
        petOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(pet.getSearchMasterMsg().getUser().getId()));
        //设置宠物
        petOrder.setPet(pet);
        //设置信息发布者
        petOrder.setUser(pet.getSearchMasterMsg().getUser());
        //设置店铺
        petOrder.setShop(pet.getShop());
        //设置收购人信息，当前登录用户
        petOrder.setEmployee(loginUser);
        //设置订单状态
        //如果为支付方式为1就代表是垫付，我们状态就应该是待报账，否则就是待打款
        petOrder.setState(pet.getPaytype()==1 ? PetHomeConstant.REIMBURSEMENT:PetHomeConstant.PAYMENT);
        return petOrder;
    }

    /**
     * 批量上架
     * @param ids
     */
    @Override
    public void batchOnSale(List<Long> ids) {
        petMapper.batchOnSale(ids);
    }

    /**
     * 批量下架
     * @param ids
     */
    @Override
    public void batchOffSale(List<Long> ids) {
        petMapper.batchOffSale(ids);
    }

    @Override
    public void adopt(Long id, Long loginInfoId) {
        //查询pet对象通过宠物id
        Pet pet = petMapper.findById(id);
        //查询登录用户通过loginInfoId
        User user = userMapper.lodaUserById(loginInfoId);
        //设置user
        pet.setUser(user);
        //设置下架状态
        pet.setState(PetHomeConstant.OFF);
        //设置下架时间
        pet.setOffsaletime(new Date());
        //修改宠物
        petMapper.update(pet);
    }
    /*批量删除*/
    @Override
    public void batchDels(List<Long> ids) {
            petMapper.batchDels(ids);
    }
}
